package cn.fl.project.transform;

import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.project.constant.PrjBscInfoConstant;
import cn.fl.project.vo.PrjPrdBscInfoVO;
import cn.fl.project.vo.PrjPrdParamConfigVO;
import cvm.store.facade.cvm.dto.VechileParaConfDTO;
import veh.src.facade.src.src.dto.VehInspectionReportDTO;
import veh.src.facade.src.src.dto.VehSrcBaseInfDTO;
import veh.src.facade.src.src.dto.VehSrcInfDTO;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者:hongyanbo
 * 时间:2018/7/16
 */
public class PrjPrdParamConfigVOTransform {

    /**
     * 适配车型库参配信息
     * @param vechileParaConfDTOS
     */
    public static PrjPrdParamConfigVO voAdapter(List<VechileParaConfDTO> vechileParaConfDTOS) {
        PrjPrdParamConfigVO vo =null;
        if (CollectionUtils.isNotEmpty(vechileParaConfDTOS)) {
            Map map = parse2Map(vechileParaConfDTOS);
            vo = new PrjPrdParamConfigVO();
            // TODO 暂时就这么多，后面再有再补
            vo.setModelId(getValueIfExist(map,"BASIC_MODELID"));//车系ID
            vo.setBrId(getValueIfExist(map,"BASIC_MAKEID"));//品牌ID
            // 轴距 BASIC_WHEELBASE
            vo.setEmiStdCd(getValueIfExist(map,"ENG_EFFLUENT"));//排放
            vo.setEmiStd(getValueIfExist(map,"ENG_EFFLUENT"));//排放
			vo.setPrdNm(getValueIfExist(map,"BASIC_PURPOSE"));// 产品名称
            vo.setPrdCd(getValueIfExist(map,"BASIC_PURPOSE")); //产品类型名称
            vo.setPrdTypCdNm(getValueIfExist(map,"BASIC_CLASSIFICATION")); //产品类型名称
            vo.setPrdMdl(getValueIfExist(map,"BASIC_BULLETIN"));// 产品型号
            vo.setPrdMdlSun(getValueIfExist(map,"BASIC_BULLETIN"));// 产品型号

            vo.setBrCdNm(getValueIfExist(map,"BASIC_MAKENAME"));//品牌
            // 挂车品牌取生产集团
            if(StringUtils.isBlank(vo.getBrCdNm())){
                vo.setBrCdNm(getValueIfExist(map,"BASIC_GROUPNAME"));
            }
            vo.setEngMdl(getValueIfExist(map,"ENG_NAME"));// 发动机
            vo.setHp(getValueIfExist(map,"ENG_HMAXPOWER")); //最大马力
            vo.setMaxHp(getValueIfExist(map,"ENG_HMAXPOWER")); //最大马力
            vo.setCabSpec(getValueIfExist(map,"PH_DRIVEROOM"));// 驾驶室
            vo.setGrboxMdl(getValueIfExist(map,"GB_GEARBOX")); // 变速箱
            String tyre_tyresize = getValueIfExist(map, "TYRE_TYRESIZE");// 轮胎规格
            String tyre_tyrenum = getValueIfExist(map, "TYRE_TYRENUM");// 轮胎数量
            vo.setWhlNum(tyre_tyrenum);
            vo.setTrlWhlSpec(tyre_tyresize + "*" + tyre_tyrenum);
            vo.setmWhlSpec(tyre_tyresize);
            vo.setPowerTypCd(getValueIfExist(map,"ENG_FUELNAME")); //动力方式 ENG_FUELNAME
            vo.setRearAxleSpec(getValueIfExist(map,"UND_REARDESC")); //后桥描述 UND_REARDESC
            String cpSize = null ;// 货箱内部尺寸
            String cp_length = getValueIfExist(map, "CP_CONTLENGTH");
            String cp_width = getValueIfExist(map, "CP_CONTWIDTH");
            String cp_height = getValueIfExist(map, "CP_CONTHEIGHT");
            String basic_lenght = getValueIfExist(map, "BASIC_LENGHT"); //客车、校车车身长度
            String cp_length_mm = null;
            String cp_width_mm = null;
            String cp_height_mm = null;
            if(cp_length == null){
                cp_length_mm = "空";
            }else{
                cp_length_mm= String.valueOf(Double.valueOf(cp_length));
            }
            if(cp_width == null){
                cp_width_mm = "空";
            }else{
                cp_width_mm= String.valueOf(Double.valueOf(cp_width));
            }
            if(cp_height == null){
                cp_height_mm= "空";
            }else{
                cp_height_mm= String.valueOf(Double.valueOf(cp_height));
            }
            cpSize = cp_length_mm + "*" + cp_width_mm + "*" +  cp_height_mm ;
            vo.setCntrSz(cpSize); //主车-合格证货箱内部尺寸(长*宽*高/毫米)
            vo.setDrvTypCd( getValueIfExist(map,"BASIC_DIRVE")); // 驱动形式
            vo.setFuelTypCd(getValueIfExist(map,"ENG_FUELNAME")); //燃料种类
            vo.setBatDeg(getValueIfExist(map,"BAT_VOLUME")); //电池容量
            vo.setBatBrand(getValueIfExist(map,"BAT_BRAND")); //电池品牌
            vo.setSeatRowCd(getValueIfExist(map,"BASIC_NSEATS")); //座位排数
            vo.setSeatQtyCd(getValueIfExist(map,"BASIC_SEATING")); //座位
            vo.setCarMdl(getValueIfExist(map,"BASIC_STYLENAME")); //车型名称
            vo.setCarStyleName(getValueIfExist(map,"BASIC_STYLENAME"));
            vo.setCarStrcCfg(getValueIfExist(map,"BASIC_STRUCTURE"));//车身结构
            vo.setGrboxCfg(getValueIfExist(map,"GB_GEARBOX")); //变速箱
            vo.setMaxSpd(getValueIfExist(map,"BASIC_MAXSPEED")); //最高车速
            vo.setOutVol(getValueIfExist(map,"ENG_EMISSION")); //排量
            vo.setPrdSeries(getValueIfExist(map,"BASIC_MODELID"));//车系
            vo.setCarCl(getValueIfExist(map,"WHO_COLOUR")); //颜色
            vo.setExtMil(getValueIfExist(map,"BAT_FULLRANGE")); //厂标续航 --新能源汽车
            if(StringUtils.isNotBlank(basic_lenght)){
                vo.setCarLen(basic_lenght); //车身长度
            }else{
                vo.setCarLen("0");
            }
            String basic_msrp = getValueIfExist(map, "BASIC_MSRP");
            if(StringUtils.isNotBlank(basic_msrp)){
                basic_msrp = String.valueOf(Double.valueOf(basic_msrp) * 1000000);//单位是万元 转化成分
            }else{
                basic_msrp = "0";
            }
            vo.setGuidePc(basic_msrp); //厂商指导价
            vo.setActCntrSz(cpSize); //上装-货箱(长*宽*高)
            vo.setLiftWayCd(getValueIfExist(map,"CP_LIFTINGFORM")); //举升形式
            vo.setDoorInfo(getValueIfExist(map,"DP_DOORNUM")); //车门数量
            vo.setWhlBase(getValueIfExist(map,"BASIC_WHEELBASE")); //轴距
            // 载货空间
            Double vol = null;
            if(cp_length!=null && cp_width!=null && !cp_length.equals("空") && !cp_width.equals("空")){
                vol = Double.parseDouble(cp_length) * Double.parseDouble(cp_width);
            }
            if(vol!=null){
                vo.setCarVol(String.valueOf(vol.intValue()));
            }
            //所属车系 BASIC_MODELNAME
            vo.setModelName(getValueIfExist(map,"BASIC_MODELNAME"));
            vo.setModelNm(getValueIfExist(map,"BASIC_MODELNAME"));
            // 所属生产集团
            //BASIC_GROUPNAME
            vo.setProductionCompany(getValueIfExist(map,"BASIC_GROUPNAME"));
            vo.setMfrNm(getValueIfExist(map,"BASIC_GROUPNAME"));
            if(StringUtils.isNotBlank(basic_lenght)){
                double v = Double.parseDouble(basic_lenght);
                if(v < 5700){
                    vo.setPrdLvlCd("轻型客车");
                }else if(v >= 10000){
                    vo.setPrdLvlCd("大型客车");
                }else{
                    vo.setPrdLvlCd("中型客车");
                }
            }
            // 挂车
            vo.setTankVol(getValueIfExist(map,"MUP_TANKVOL")); //罐体容积
            vo.setBridgeNum(getValueIfExist(map,"MUP_BRIDGENUM")); //车桥数量
            vo.setBridgeBrand(  getValueIfExist(map,"MUP_BRIDGEBRAND")); //车桥品牌
            String who_thickfloor = getValueIfExist(map, "WHO_THICKFLOOR");// 货箱底板厚度
            String who_thicksidebox = getValueIfExist(map, "WHO_THICKSIDEBOX");//厢体边板厚度
            if(StringUtils.isBlank(who_thickfloor)){
                who_thickfloor = "空";
            }
            if(StringUtils.isBlank(who_thicksidebox)){
                who_thicksidebox="空";
            }
            vo.setThickness(who_thickfloor + "*" + who_thicksidebox); //上装/挂车-厚度(底*边)(毫米)
            vo.setMatQalCd(getValueIfExist(map, "TANK_MATERIAL")); //油箱/气罐材质  TANK_MATERIAL
        }
        return vo;
    }

    private static String getValueIfExist(Map<String,VechileParaConfDTO> map,String key){
        VechileParaConfDTO vechileParaConfDTO = map.get(key);
        if(vechileParaConfDTO != null){
            return vechileParaConfDTO.getParaConfValue();
        }else{
            return null;
        }
    }
    /**
     * 转化成map
     * @param vechileParaConfDTOS
     */
    private static Map<String,VechileParaConfDTO> parse2Map(List<VechileParaConfDTO> vechileParaConfDTOS) {
        Map res = new HashMap<>();
        for (VechileParaConfDTO vechileParaConfDTO : vechileParaConfDTOS) {
            String key = vechileParaConfDTO.getParaConfEnm();
            res.put(key,vechileParaConfDTO);
        }
        return res ;
    }

    public static PrjPrdBscInfoVO toPrjPrdBscInfoVO(PrjPrdParamConfigVO vo){

        return null;
    }

    public static PrjPrdParamConfigVO voVehSrcAdapter(VehSrcInfDTO vehSrcInf, VehSrcBaseInfDTO vehSrcBaseInfDTO, List<VechileParaConfDTO> vechileParaConfDTOS, VehInspectionReportDTO vehInspectionReportDTO) {
        PrjPrdParamConfigVO vo = new PrjPrdParamConfigVO();
        //
        if(null!=vehSrcInf){
            //车源信息
            vo.setCarSourceId(vehSrcInf.getId());
            vo.setPrdTypCd(vehSrcInf.getCarType());
            vo.setPrdTypCdNm(vehSrcInf.getCarTypeNm());
            vo.setVin(vehSrcInf.getVin());//vin
            vo.setBrCdNm(vehSrcInf.getMakeName());
            vo.setStyleId(vehSrcInf.getParaId());//车型id
            vo.setModelNm(vehSrcInf.getModelName());
            vo.setModelName(vehSrcInf.getModelName());
            vo.setMfrId(vehSrcInf.getMfrId());//厂商id
            vo.setMfrNm(vehSrcInf.getMfrNm());//经销商名称
            vo.setParkingLot(vehSrcInf.getParkingLot());//车辆停放地
            vo.setExtMil(vehSrcInf.getMil());//行驶里程
            vo.setDrvTypCd(vehSrcInf.getStyleDriveMode());//驱动形式
            vo.setEngMdl(vehSrcInf.getEngMdl());//发动机型号
            if(null!=vehSrcInf.getEngineMaxHorsepower()){
                vo.setHp(String.valueOf(vehSrcInf.getEngineMaxHorsepower()));//马力
            }
            if(null!=vehSrcInf.getEngineMaxHorsepower()){
                vo.setMaxHp(String.valueOf(vehSrcInf.getEngineMaxHorsepower()));//最大马力
            }
            vo.setCarNo(vehSrcInf.getCarNo());//车源编号(六位)
            vo.setCarStyleName(vehSrcInf.getStyleName());//车型名称
            vo.setWhlBase(vehSrcInf.getStyleWheelbase());//轴距
            vo.setGrboxTypCd(vehSrcInf.getGerrboxBrand());
            vo.setSurveyorId(vehSrcInf.getSurveyorId());//检测师ID
            vo.setSurveyorNm(vehSrcInf.getSurveyorNm());//检测师名称
            vo.setPrdNm(vehSrcInf.getPrdNm());//产品名称
            vo.setLicNo(vehSrcInf.getLicensePlateNumber());//车牌号
            vo.setMil(vehSrcInf.getMil());
            vo.setDetectionDate(vehSrcInf.getDetectionPassTm());
            vo.setVehCheckCode(vehSrcInf.getCheckCarMode());
            vo.setVin(vehSrcInf.getVin());
            vo.setGerrBoxBrand(vehSrcInf.getGerrboxBrand());
            vo.setStyleEngine(vehSrcInf.getStyleEngine());
            vo.setEmiStdCd(vehSrcInf.getEmissionStandard());//排放
            vo.setEmiStd(vehSrcInf.getEmissionStandard());//排放
            vo.setReleaseDate(vehSrcInf.getReleaseDate());
            vo.setPrvCode(vehSrcInf.getPrvCode());
            vo.setCityCode(vehSrcInf.getCityCode());
            vo.setPrdCd(vehSrcInf.getCarLevelType());
        }
        if (null!=vehInspectionReportDTO){
            vo.setPdfFilePath(vehInspectionReportDTO.getPdfFilePath());
            vo.setAbsLocPath(vehInspectionReportDTO.getRootPath()+vehInspectionReportDTO.getPdfFilePath());
        }
        if(null!=vehSrcBaseInfDTO){
            vo.setGrboxCfg(vehSrcBaseInfDTO.getGerrbox());
            vo.setGrboxMdl(vehSrcBaseInfDTO.getGerrbox());
            if(null!=vehSrcBaseInfDTO.getLightRepeat()){
                vo.setRearAxleSpec(vehSrcBaseInfDTO.getLightRepeat().toString());//后桥
            }
            vo.setCarLen(vehSrcBaseInfDTO.getContainerLength());//货箱长
            /*String cpSize = null ;// 货箱内部尺寸
            String cp_length = vehSrcBaseInfDTO.getContainerLength();//货箱长
            String cp_width = vehSrcBaseInfDTO.getContainerWidth();//货箱宽
            String cp_height = vehSrcBaseInfDTO.getContainerHeight();//货箱高
            String cp_length_mm = null;
            String cp_width_mm = null;
            String cp_height_mm = null;
            if(cp_length == null){
                cp_length_mm = "空";
            }else{
                cp_length_mm= String.valueOf(Double.valueOf(cp_length));
            }
            if(cp_width == null){
                cp_width_mm = "空";
            }else{
                cp_width_mm= String.valueOf(Double.valueOf(cp_width));
            }
            if(cp_height == null){
                cp_height_mm= "空";
            }else{
                cp_height_mm= String.valueOf(Double.valueOf(cp_height));
            }
            cpSize = cp_length_mm + "*" + cp_width_mm + "*" +  cp_height_mm ;
            vo.setCntrSz(cpSize); //主车-合格证货箱内部尺寸(长*宽*高/毫米)*/
            vo.setCarCl(vehSrcBaseInfDTO.getCarClr());//车辆颜色
            if(null!=vehSrcBaseInfDTO.getFuelTypeId()){
                vo.setFuelTypCd(vehSrcBaseInfDTO.getFuelTypeId().toString());//燃油类型
            }
            vo.setCabSpec(vehSrcBaseInfDTO.getPhDriveroom());//驾驶室规格
            vo.setLiftWayCd(vehSrcBaseInfDTO.getCpLiftingform()); //举升形式
            vo.setmWhlSpec(vehSrcBaseInfDTO.getTyreTyresize());//轮胎规格
            vo.setWhlNum(vehSrcBaseInfDTO.getTyreTyrenum());//轮胎数量
            vo.setTrlWhlSpec(vehSrcBaseInfDTO.getTyreTyresize() + "*" + vehSrcBaseInfDTO.getTyreTyrenum());
            vo.setThickness(vehSrcBaseInfDTO.getCpContthickness()); //上装/挂车-厚度(底*边)(毫米)
            vo.setDoorInfo(vehSrcBaseInfDTO.getDpDoornumtype()); //车门数量
            vo.setTankVol(vehSrcBaseInfDTO.getMupVol()); //罐体容积
        }

        if (CollectionUtils.isNotEmpty(vechileParaConfDTOS)) {
            Map map = parse2Map(vechileParaConfDTOS);
            // 轴距 BASIC_WHEELBASE
            /*vo.setEmiStdCd(getValueIfExist(map,"ENG_EFFLUENT"));//排放
            vo.setEmiStd(getValueIfExist(map,"ENG_EFFLUENT"));//排放*/
            vo.setPrdMdl(getValueIfExist(map,"BASIC_BULLETIN"));// 产品型号
            vo.setPrdMdlSun(getValueIfExist(map,"BASIC_BULLETIN"));// 产品型号
            vo.setPowerTypCd(getValueIfExist(map,"ENG_FUELNAME")); //动力方式 ENG_FUELNAME
            vo.setBatDeg(getValueIfExist(map,"BAT_VOLUME")); //电池容量
            vo.setBatBrand(getValueIfExist(map,"BAT_BRAND")); //电池品牌
            vo.setSeatRowCd(getValueIfExist(map,"BASIC_NSEATS")); //座位排数
            vo.setSeatQtyCd(getValueIfExist(map,"BASIC_SEATING")); //座位
            vo.setCarMdl(getValueIfExist(map,"BASIC_STYLENAME")); //车型名称
            vo.setCarStrcCfg(getValueIfExist(map,"BASIC_STRUCTURE"));//车身结构
            vo.setMaxSpd(getValueIfExist(map,"BASIC_MAXSPEED")); //最高车速
            vo.setOutVol(getValueIfExist(map,"ENG_EMISSION")); //排量
            vo.setPrdSeries(getValueIfExist(map,"BASIC_MODELID"));//车系
            /*vo.setPrdCd(getValueIfExist(map,"STYLE_PURPOSE_ID"));*/
            String cpSize = null ;// 货箱内部尺寸
            String cp_length = getValueIfExist(map, "CP_CONTLENGTH");//货箱长
            String cp_width = getValueIfExist(map, "CP_CONTWIDTH");//货箱宽
            String cp_height = getValueIfExist(map, "CP_CONTHEIGHT");//货箱高
            String cp_length_mm = null;
            String cp_width_mm = null;
            String cp_height_mm = null;
            if(cp_length == null){
                cp_length_mm = "空";
            }else{
                cp_length_mm= String.valueOf(Double.valueOf(cp_length));
            }
            if(cp_width == null){
                cp_width_mm = "空";
            }else{
                cp_width_mm= String.valueOf(Double.valueOf(cp_width));
            }
            if(cp_height == null){
                cp_height_mm= "空";
            }else{
                cp_height_mm= String.valueOf(Double.valueOf(cp_height));
            }
            cpSize = cp_length_mm + "*" + cp_width_mm + "*" +  cp_height_mm;
            vo.setCntrSz(cpSize); //主车-合格证货箱内部尺寸(长*宽*高/毫米)
            // 载货空间
            Double vol = null;
            if(cp_length!=null && cp_width!=null && !cp_length.equals("空") && !cp_width.equals("空")){
                vol = Double.parseDouble(cp_length) * Double.parseDouble(cp_width);
            }
            if(vol!=null){
                vo.setCarVol(String.valueOf(vol.intValue()));
            }
            vo.setCpContlength(cp_length);//货箱长度
            vo.setBridgeNum(getValueIfExist(map,"MUP_BRIDGENUM")); //车桥数量
            vo.setBridgeBrand(  getValueIfExist(map,"MUP_BRIDGEBRAND")); //车桥品牌
            vo.setMatQalCd(getValueIfExist(map, "TANK_MATERIAL")); //油箱/气罐材质  TANK_MATERIAL
            vo.setPurposeid(getValueIfExist(map, "BASIC_PURPOSE"));//车用途ID
        }

        return  vo;
    }
}
